package cc.comac.mom.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.EquipPatrolCheckPlanService;
import cc.comac.mom.bean.EquipPatrolCheckPlan;
import cc.comac.mom.bean.EquipPatrolCheckPlanBean;
import cc.comac.mom.bean.EquipPatrolCheckPlanItem;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.datahandlers.entity.EquipPatrolCheckPlanDataHandler;
import cc.comac.mom.datahandlers.entity.EquipPatrolCheckPlanItemDataHandler;
import cc.comac.mom.entity.EquipPatrolCheckPlanEntity;
import cc.comac.mom.util.ResultBean;

@Service
@RestController
@Transactional
public class EquipPatrolCheckPlanServiceImpl implements EquipPatrolCheckPlanService{

	@Autowired
	private EquipPatrolCheckPlanDataHandler equipPatrolCheckPlanDataHandler;
	@Autowired
	private EquipPatrolCheckPlanItemDataHandler equipPatrolCheckPlanItemDataHandler;
	@Override
	public ResultBean insert(@RequestBody EquipPatrolCheckPlan obj) {
		ResultBean resultBean = new ResultBean(true);
		ResultBean insert = equipPatrolCheckPlanDataHandler.insert(obj);
		if(!insert.success) {
			return insert;
		}
		List<EquipPatrolCheckPlanItem> listItem = obj.getListItem();
		if(listItem != null && listItem.size()>0) {
			for(EquipPatrolCheckPlanItem item:listItem) {
				item.getInnerData().setPatrolCheckId(obj.getInnerData().getId());
			}
			ResultBean insertBatch = equipPatrolCheckPlanItemDataHandler.insertBatch(listItem);
			if(!insertBatch.success) {
				throw new RuntimeException(insertBatch.getMessage());
			}
		}
		return resultBean;
	}

	@Override
	public ResultBean delete(@RequestBody List<EquipPatrolCheckPlan> list) {
		ResultBean batchDeleteByMaintainPlanId = equipPatrolCheckPlanDataHandler.delete(list);
		return batchDeleteByMaintainPlanId;
	}
	@Override
	public ResultBean update(@RequestBody EquipPatrolCheckPlan obj) {
		ResultBean resultBean = new ResultBean(true);
		ResultBean update = equipPatrolCheckPlanDataHandler.update(obj);
		if(!update.success) {
			throw new RuntimeException(update.getMessage());	
		}
		List<EquipPatrolCheckPlanItem> listItem = obj.getListItem();
		if(listItem != null && listItem.size()>0) {
			for(EquipPatrolCheckPlanItem item:listItem) {
				item.getInnerData().setPatrolCheckId(obj.getInnerData().getId());
			}
			ResultBean insertBatch = equipPatrolCheckPlanItemDataHandler.insertBatch(listItem);
			if(!insertBatch.success) {
				throw new RuntimeException(insertBatch.getMessage());
			}
		}
		return resultBean;
	}

	@Override
	public ResultBean planIssue(@RequestBody List<EquipPatrolCheckPlanEntity> list) {
		// TODO Auto-generated method stub
		return equipPatrolCheckPlanDataHandler.planIssue(list);
	}

	@Override
	public ResultBean selectByFields(@RequestBody EquipPatrolCheckPlanBean obj) {
		ResultBean resultBean = new ResultBean(true);
		ResultDev resultDev = equipPatrolCheckPlanDataHandler.selectByFields(obj);
		resultBean.setObject(resultDev);
		return resultBean;
	}

}
